package com.nbjtjc.safe.service.cost;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.cost.CostAccountRegMapper;
import com.nbjtjc.safe.mapper.cost.CostBudgetPlanMapper;
import com.nbjtjc.safe.mapper.edu.EduCertificateMapper;
import com.nbjtjc.safe.mapper.edu.EduCertificateRetrainMapper;
import com.nbjtjc.safe.mapper.edu.EduDailyExamineListMapper;
import com.nbjtjc.safe.mapper.edu.EduDailyInfoMapper;
import com.nbjtjc.safe.mapper.edu.EduDailyRecordMapper;
import com.nbjtjc.safe.mapper.edu.EduEntryTrainingMapper;
import com.nbjtjc.safe.mapper.edu.EduPlanListMapper;
import com.nbjtjc.safe.mapper.edu.EduReassignmentMapper;
import com.nbjtjc.safe.model.cost.CostAccountReg;
import com.nbjtjc.safe.model.cost.CostBudgetPlan;
import com.nbjtjc.safe.model.edu.EduCertificate;
import com.nbjtjc.safe.model.edu.EduDailyInfo;
import com.nbjtjc.safe.model.edu.EduEntryTraining;
import com.nbjtjc.safe.model.edu.EduReassignment;
import com.nbjtjc.safe.util.MyShiroUtils;

@Service
public class FrontPageService {
	  @Autowired
	    private EduPlanListMapper eduPlanListMapper;

	    @Autowired
	    private EduCertificateMapper eduCertificateMapper;
	    @Autowired
	    EduCertificateRetrainMapper eduCertificateRetrainMapper;

	    @Autowired
	    private EduEntryTrainingMapper eduEntryTrainingMapper;
	    @Autowired
	    private EduReassignmentMapper eduReassignmentMapper;

	    @Autowired
	    private EduDailyInfoMapper eduDailyInfoMapper;

	    @Autowired
	    private EduDailyRecordMapper eduDailyRecordMapper;

	    @Autowired
	    private EduDailyExamineListMapper eduDailyExamineListMapper;
		@Autowired
		  private CostAccountRegMapper costAccountRegMapper;
		
		@Autowired
		  private CostBudgetPlanMapper costBudgetPlanMapper;
	    
	    //教育培训情况
	    public Map eduCondition(Date year){
	       	Calendar   calendar   =   new   GregorianCalendar();
	        
	        //一年的时间范围
	    	calendar.setTime(year);
	    	calendar.add(calendar.YEAR, 1);
	    	
	    	Map resultMap = new HashMap();
	    	
	    	//三级培训
	    	List<Integer> entryIds=new ArrayList();
	    	
	    	EduEntryTraining eduEntryTrainingSample = new EduEntryTraining();
	    	List<EduEntryTraining> eduEntryTrainings = new ArrayList();
	    	eduEntryTrainingSample.setStartDate(year);
	    	eduEntryTrainingSample.setEndDate(calendar.getTime());
	    	eduEntryTrainingSample.setCompanyId(MyShiroUtils.getCompanyId());

	    	int eduEntryTrainingTotal = 0;
	    	int eduEntryTrainingFail = 0;

	    	eduEntryTrainings = eduEntryTrainingMapper.search(eduEntryTrainingSample);
	    	if(CollectionUtil.isNotEmpty(eduEntryTrainings)){
	    		eduEntryTrainingTotal = eduEntryTrainings.size();
		    	for(int i=0;i<eduEntryTrainingTotal;i++){
		    		if(ObjectUtil.isNull(eduEntryTrainings.get(i).getScore())){
		    			eduEntryTrainings.get(i).setScore(0);
		    		}
		    		if(eduEntryTrainings.get(i).getScore()<60){
		    			eduEntryTrainingFail++;
		    		}
		    	}
	    	}
	    	resultMap.put("eduEntryTrainingTotal", eduEntryTrainingTotal);
	    	resultMap.put("eduEntryTrainingFail", eduEntryTrainingFail);

	    	
	    	//持证培训
	    	List<Integer> certIds=new ArrayList();
	    	EduCertificate eduCertificateSample = new EduCertificate();
	    	List<EduCertificate> eduCertificates = new ArrayList();
	    	eduCertificateSample.setStartDate(year);
	    	eduCertificateSample.setEndDate(calendar.getTime());
	    	eduCertificateSample.setCompanyId(MyShiroUtils.getCompanyId());

	        eduCertificates = eduCertificateMapper.search(eduCertificateSample);
	        int eduCertificateTotal = 0;
	        if(CollectionUtil.isNotEmpty(eduCertificates)){
	        	eduCertificateTotal = eduCertificates.size();
	        }
	    	resultMap.put("eduCertificateTotal", eduCertificateTotal);
	    	//日常培训
	    	List<Integer> dailyIds=new ArrayList();

	    	EduDailyInfo eduDailyInfoSample = new EduDailyInfo();
	    	List<EduDailyInfo> eduDailyInfos = new ArrayList();
	    	eduDailyInfoSample.setStartDate(year);
	    	eduDailyInfoSample.setEndDate(calendar.getTime());
	    	eduDailyInfoSample.setCompanyId(MyShiroUtils.getCompanyId());

	    	eduDailyInfos = eduDailyInfoMapper.search(eduDailyInfoSample);
	    	int eduDailyInfoTotal = 0;
	    	int eduDailyInfoFail = 0;
	    	
	    	eduDailyInfoTotal = eduDailyInfoMapper.getTotalPeople(year,calendar.getTime(),MyShiroUtils.getCompanyId());
	    	eduDailyInfoFail = eduDailyInfoMapper.getFailPeople(year,calendar.getTime(),MyShiroUtils.getCompanyId());
	    	resultMap.put("eduDailyInfoTotal", eduDailyInfoTotal);
	    	resultMap.put("eduDailyInfoFail", eduDailyInfoFail);
	    	//转岗培训
	        List<Integer> reasIds=new ArrayList();
    	
	    	EduReassignment eduReassignmentSample = new EduReassignment();
	    	List<EduReassignment> eduReassignments = new ArrayList();
	    	eduReassignmentSample.setStartDate(year);
	    	eduReassignmentSample.setEndDate(calendar.getTime());
	    	eduReassignmentSample.setCompanyId(MyShiroUtils.getCompanyId());
	
	    	eduReassignments = eduReassignmentMapper.search(eduReassignmentSample);
	    	int eduReassignmentTotal = 0;
	    	int eduReassignmentFail = 0;
	    	
	    	if(CollectionUtil.isNotEmpty(eduReassignments)){
	    		eduReassignmentTotal = eduReassignments.size();
		    	for(int i=0;i<eduReassignmentTotal;i++){
		    		if(ObjectUtil.isNull(eduReassignments.get(i).getScore())){
		    			eduReassignments.get(i).setScore(0);
		    		}
		    		if(eduReassignments.get(i).getScore()<60){
		    			eduReassignmentFail++;
		    		}
		    	}
	    	}
	    	resultMap.put("eduReassignmentTotal", eduReassignmentTotal);
	    	resultMap.put("eduReassignmentFail", eduReassignmentFail);
	    	return resultMap;
	    }
	    
	    //安全投入情况
	    public Map costCondition(Date year){
	    	Map resultMap = new HashMap();

	    	//费用金额
        	//根据年份找出预算投入主表ID
	    	float regTotal = 0;
    		CostAccountReg costAccountRegSample = new CostAccountReg();
    		costAccountRegSample.setYear(year);
    		costAccountRegSample.setCompanyId(MyShiroUtils.getCompanyId());

        	List<CostAccountReg> costAccountRegs = costAccountRegMapper.search(costAccountRegSample);
        	if(CollectionUtil.isNotEmpty(costAccountRegs)){
        		if(ObjectUtil.isNotNull(costAccountRegs.get(0).getTotalAccount())){
            		regTotal = costAccountRegs.get(0).getTotalAccount();
        		}
        	}

//        	if(CollectionUtil.isNotEmpty(costAccountRegs)){
//        		result.setFalse("该预算投入记录为空");
//        		return result;
//        	}else if(costAccountRegs.size()>1){
//        		result.setFalse("该预算投入记录有多个，请检查");
//        		return result;
//        	}else{
//        		regTotal = costAccountRegs.get(0).getTotalAccount();
//        	}
	    	//占预算比例
	      	//根据年份找出预算计划主表ID
        	float planBudget = 0;
	    	CostBudgetPlan costBudgetPlanSample = new CostBudgetPlan();
    		costBudgetPlanSample.setYear(year);
    		costBudgetPlanSample.setDeptId(MyShiroUtils.getCompanyId());
        	List<CostBudgetPlan> costBudgetPlans = costBudgetPlanMapper.search(costBudgetPlanSample);
        	if(CollectionUtil.isNotEmpty(costBudgetPlans)){
        		if(ObjectUtil.isNotNull(costBudgetPlans.get(0).getTotalBudget())){
        			planBudget = costBudgetPlans.get(0).getTotalBudget();
        		}
        	}

//        	if(CollectionUtil.isEmpty(costBudgetPlans)){
//        		result.setFalse("该预算计划记录为空");
//        		return result;
//        	}else if(costBudgetPlans.size()>1){
//        		result.setFalse("该预算计划记录有多个，请检查");
//        		return result;
//        	}else{
//        		planBudget = costBudgetPlans.get(0).getTotalBudget();
//        	}
        	 float budgetRate = 0;
        	if(planBudget!=0){
        		budgetRate = regTotal/planBudget;
        	}

	    	resultMap.put("regTotal", regTotal);
	    	resultMap.put("budgetRate", budgetRate);
	    	
	    	return resultMap;
	    }
}
